package com.wei.weblog.mr;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.ObjectUtils;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

public class WebLogInfo implements WritableComparable<WebLogInfo>{
	private boolean isValid;
	private String remoteIp;
	private String remoteUserInfo;
	private String requestTime;
	private String requestMethod;
	private String requestUrl;
	private String requestProtocol;
	private String responseCode;
	private String bodySendBytes;
	private String refererUrl;
	private String userAgent;
	
	public WebLogInfo() {
		
	}
	
	

	public void set(boolean isValid, String remoteIp, String remoteUserInfo,
			String requestTime,String requestUrl, String requestMethod, String requestProtocol,
			String responseCode, String bodySendBytes, String refererUrl,
			String userAgent) {
		this.isValid = isValid;
		this.remoteIp = remoteIp;
		this.remoteUserInfo = remoteUserInfo;
		this.requestTime = requestTime;
		this.requestUrl = requestUrl;
		this.requestMethod = requestMethod;
		this.requestProtocol = requestProtocol;
		this.responseCode = responseCode;
		this.bodySendBytes = bodySendBytes;
		this.refererUrl = refererUrl;
		this.userAgent = userAgent;
	}



	public boolean isValid() {
		return isValid;
	}

	public void setValid(boolean isValid) {
		this.isValid = isValid;
	}

	
	
	public String getRequestUrl() {
		return requestUrl;
	}



	public void setRequestUrl(String requestUrl) {
		this.requestUrl = requestUrl;
	}



	public String getRemoteIp() {
		return remoteIp;
	}

	public void setRemoteIp(String remoteIp) {
		this.remoteIp = remoteIp;
	}

	public String getRemoteUserInfo() {
		return remoteUserInfo;
	}

	public void setRemoteUserInfo(String remoteUserInfo) {
		this.remoteUserInfo = remoteUserInfo;
	}

	public String getRequestTime() {
		return requestTime;
	}

	public void setRequestTime(String requestTime) {
		this.requestTime = requestTime;
	}

	public String getRequestMethod() {
		return requestMethod;
	}

	public void setRequestMethod(String requestMethod) {
		this.requestMethod = requestMethod;
	}

	public String getRequestProtocol() {
		return requestProtocol;
	}

	public void setRequestProtocol(String requestProtocol) {
		this.requestProtocol = requestProtocol;
	}

	public String getResponseCode() {
		return responseCode;
	}

	public void setResponseCode(String responseCode) {
		this.responseCode = responseCode;
	}

	public String getBodySendBytes() {
		return bodySendBytes;
	}

	public void setBodySendBytes(String bodySendBytes) {
		this.bodySendBytes = bodySendBytes;
	}

	public String getRefererUrl() {
		return refererUrl;
	}

	public void setRefererUrl(String refererUrl) {
		this.refererUrl = refererUrl;
	}

	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}



	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.getRemoteIp());
		sb.append("\001").append(this.getRemoteUserInfo());
		sb.append("\001").append(this.getRequestTime());
		sb.append("\001").append(this.getRequestMethod());
		sb.append("\001").append(this.getRequestUrl());
		sb.append("\001").append(this.getRequestProtocol());
		sb.append("\001").append(this.getResponseCode());
		sb.append("\001").append(this.getBodySendBytes());
		sb.append("\001").append(this.getRefererUrl());
		sb.append("\001").append(this.getUserAgent());
		return sb.toString();
	}



	@Override
	public void write(DataOutput out) throws IOException {
		out.writeBoolean(this.isValid);
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.remoteIp, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.remoteUserInfo, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.requestTime, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.requestUrl, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.requestMethod, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.requestProtocol, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.responseCode, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.bodySendBytes, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.refererUrl, ""));
		out.writeUTF((String) ObjectUtils.defaultIfNull(this.userAgent, ""));
	}



	@Override
	public void readFields(DataInput in) throws IOException {
		this.isValid=in.readBoolean();
		this.remoteIp=in.readUTF();
		this.remoteUserInfo=in.readUTF();
		this.requestTime=in.readUTF();
		this.requestUrl=in.readUTF();
		this.requestMethod=in.readUTF();
		this.requestProtocol=in.readUTF();
		this.responseCode=in.readUTF();
		this.bodySendBytes=in.readUTF();
		this.refererUrl=in.readUTF();
		this.userAgent=in.readUTF();
	}



	@Override
	public int compareTo(WebLogInfo o) {
		String requestTime2 = this.getRequestTime();
		String requestTime3 = o.getRequestTime();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
		try {
			Date parse = df.parse(requestTime2);
			Date parse2 = df.parse(requestTime3);
			return parse2.compareTo(parse);
		} catch (ParseException e) {
			return 0;
		}
	}
	
	
}
